//
//  JAddShopCarNumberView.m
//  HeadIndustry
//
//  Created by jun on 2017/8/31.
//  Copyright © 2017年 jun. All rights reserved.
//

#import "JAddShopCarNumberView.h"

@interface JAddShopCarNumberView()

@property (nonatomic, strong) UIButton * leftBtn;
@property (nonatomic, strong) UIButton * rightBtn;


@end

@implementation JAddShopCarNumberView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        self.opaque = NO;
        _currentNum = 1;
        _collectNum = 50;
        [self setUpView];
    }
    return  self;
}

- (void)setUpView {
    UIColor *lineColor = kRGBColor(150, 150, 150);
    
    YYLabel *title = [YYLabel new];
    title.text = @"数量";
    title.left = 10;
    title.top = 10;
    title.size = CGSizeMake(80, 30);
    title.font = kFONT_TITLE(16);
    title.textColor = [UIColor blackColor];
    [self addSubview:title];
    
    UIButton *leftNumBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftNumBtn.frame = CGRectMake(10, CGRectGetMaxY(title.frame) +10, 30 , 30);
    [leftNumBtn setTitle:@"-" forState:UIControlStateNormal];
    [leftNumBtn setTitleColor:lineColor forState:UIControlStateNormal];
    
    leftNumBtn.layer.borderColor = lineColor.CGColor;
    leftNumBtn.layer.borderWidth = 0.5;

    
    [leftNumBtn addTarget:self action:@selector(leftNumBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:leftNumBtn];
    _leftBtn = leftNumBtn;
    
    YYLabel *cardNum = [[YYLabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(leftNumBtn.frame), CGRectGetMinY(leftNumBtn.frame), 30, 30)];
    cardNum.text = [NSString stringWithFormat:@"%ld",(long)_currentNum];
    cardNum.font = [UIFont systemFontOfSize:14];
    cardNum.textColor = [UIColor blackColor];
    cardNum.layer.borderColor = lineColor.CGColor;
    cardNum.layer.borderWidth = 0.5;

    cardNum.textAlignment = NSTextAlignmentCenter;
    [self addSubview:cardNum];
    _numberLa = cardNum;
    
    UIButton *rightNumBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightNumBtn.frame = CGRectMake(CGRectGetMaxX(cardNum.frame), CGRectGetMinY(leftNumBtn.frame), 30, 30);
    [rightNumBtn setTitle:@"＋" forState:UIControlStateNormal];
    [rightNumBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//    [rightNumBtn setImage:[UIImage imageNamed:@"jia"] forState:UIControlStateNormal];
    rightNumBtn.layer.borderColor = lineColor.CGColor;
    rightNumBtn.layer.borderWidth = 0.5;


    [rightNumBtn addTarget:self action:@selector(rightNumBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:rightNumBtn];
    _rightBtn = rightNumBtn;
    
    UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    doneBtn.top = self.height - 60;
    doneBtn.centerX = self.centerX - 70;
    doneBtn.size = CGSizeMake(140, 40);
    doneBtn.backgroundColor = [UIColor redColor];
    [doneBtn setTitle:@"确定" forState:UIControlStateNormal];
    [doneBtn addTarget:self action:@selector(doneBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:doneBtn];
    
}

- (void)setDetailModel:(JProductDetailModel *)detailModel
{
//    _detailModel = detailModel;
    self.currentNum = _detailModel.StoreProductCount;
}

- (void)setCollectNum:(NSInteger)collectNum
{
    _collectNum = collectNum;
}

//-
- (void)leftNumBtnClick:(UIButton *)btn {
    
    if (_currentNum > 1) {
        _currentNum -= 1;
    }
    
    if (_collectNum > 1) {
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }
    else
    {
        [btn setTitleColor:kRGBColor(150, 150, 150) forState:UIControlStateNormal];
    }
    
    [self changeNumBerLabel:_currentNum];
}
//+
- (void)rightNumBtnClick:(UIButton *)btn {
    if (_currentNum < _collectNum) {
        _currentNum += 1;
    }
    
    if (_currentNum > 1) {
        [_leftBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    }
    
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    [self changeNumBerLabel:_currentNum];
}

// 确定提交按钮
- (void)doneBtnClick:(UIButton *)btn {
    if (_BtnBlock) {
        _BtnBlock();
    }
}

- (void)changeNumBerLabel:(NSInteger)num
{
    if (_productNumBlock) {
        _productNumBlock(_currentNum);
    }
    _numberLa.text = [NSString stringWithFormat:@"%ld",(long)num];
}

@end
